package com.cq.cqmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.common.utils.PageUtils;
import com.cq.common.utils.Query;
import com.cq.cqmall.product.dao.AttrGroupDao;
import com.cq.cqmall.product.entity.AttrEntity;
import com.cq.cqmall.product.entity.AttrGroupEntity;
import com.cq.cqmall.product.service.AttrGroupService;
import com.cq.cqmall.product.service.AttrService;
import com.cq.cqmall.product.vo.AttrGroupWithAttrsVO;
import com.cq.cqmall.product.vo.SkuItemVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = Wrappers.lambdaQuery(AttrGroupEntity.class);
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.and(obj -> obj.eq(AttrGroupEntity::getAttrGroupId, key)
                    .or()
                    .like(AttrGroupEntity::getAttrGroupName, key));
        }
        if (catelogId.equals(0L)) {
            return new PageUtils(
                    this.page(
                            new Query<AttrGroupEntity>().getPage(params),
                            queryWrapper
                    )
            );
        }
        queryWrapper.eq(AttrGroupEntity::getCatelogId, catelogId);
        return new PageUtils(
                this.page(
                        new Query<AttrGroupEntity>().getPage(params),
                        queryWrapper
                )
        );
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(
                Wrappers.lambdaQuery(AttrGroupEntity.class)
                        .eq(AttrGroupEntity::getCatelogId, catelogId)
        );
        return attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVO attrGroupWithAttrsVO = new AttrGroupWithAttrsVO();
            BeanUtils.copyProperties(group, attrGroupWithAttrsVO);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithAttrsVO.getAttrGroupId());
            attrGroupWithAttrsVO.setAttrs(attrs);
            return attrGroupWithAttrsVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SkuItemVO.SpuItemAttrGroupVO> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 1、查出当前spu对应的所有属性的分组信息以及当前分组
        List<SkuItemVO.SpuItemAttrGroupVO> spuItemAttrGroupVos = this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return null;
    }

}